<template>
  <div class="toc-body">
      <a-anchor :offsetTop="50" :showInkInFixed="true" class="toc-affix" :affix="false">
        <template v-for="t in toc">
          <a-anchor-link :title="t.title" :href="t.url" :key="t.url">
            <template v-for="tt in t.children">
              <a-anchor-link :title="tt.title" :href="tt.url" :key="tt.url"/>
            </template>
          </a-anchor-link>
        </template>
      </a-anchor>

    <div class="container">
      <article>
        <div class="header">
          <p class="title">{{article.title}}</p>
          <p class="info">
            <span><a-icon type="calendar"/> {{article.createdAt}} </span>
            <span><a-icon type="user"/> {{article.user.username}}</span>
          </p>
          <div class="tags">
            <template v-for="tag in article.tags">
              <a-tag :key="tag.name">{{tag.name}}</a-tag>
            </template>
          </div>
        </div>
        <div class="markdown-body content">
          <div v-html="htmlContent"></div>
        </div>
      </article>

      <div class="comment">

      </div>
    </div>
  </div>
</template>

<script>
  import api from '../api'
  import {Compiler} from '@/core/compiler'

  const c = new Compiler()

  export default {
    name: 'article-detail',
    data () {
      return {
        drawlerVisible: true,
        toc: [],
        article: {
          title: '',
          content: '',
          createdAt: '',
          tags: [],
          user: {},
        },
        htmlContent: ''
      }
    },
    mounted () {
      let id = this.$route.params.id
      this.getArticle(id)
    },
    methods: {
      getArticle (id) {
        api.getArticle(id).then(resp => {
          this.article = resp.data.data
          this.htmlContent = c.compile(this.article.content)
          this.toc = c.toc
        })
      }
    }
  }
</script>

<style lang="less">
  .markdown-body {
    h1 {
      border-bottom: 1px solid #ddd;
      margin-top: 2em;
    }

    h2 {
      border-bottom: 1px solid #ddd;
      margin-top: 2em;
    }

    h3 {
      margin-top: 1.5em;
    }

    code {
      font-family: "Roboto Mono", Monaco, courier, monospace;
      font-size: 0.95rem;
      color: #e96900;
      padding: 3px 5px;
      margin: 0 2px;
      border-radius: 2px;
      /*white-space: nowrap;*/
    }
    pre > code {
      color: #3b3e43;
      border-radius: 5px;
      overflow: auto;
      padding: 2px;
      margin: 2px 0;
    }
    img {
      max-width: 100%;
      height: auto;
    }
    a {
      word-break: break-all;
    }
  }
</style>

<style lang="less" scoped>
  .toc-affix {
    position: fixed;
    top: 100px;
    right: 20px;
    height: 100%;
    width: 300px;
    font-size: 14px !important;
    transition: all .2s;
    max-height: calc(100vh - 150px) !important;
  }
  .toc-body {
    padding-right: 0;
    transition: all .2s;
  }

  @media only screen and (max-width: 1600px) {
    .toc-body {
      padding-right: 320px;
    }
  }

  @media only screen and (max-width: 768px) {
    .toc-affix {
      right: -400px;
    }
    .toc-body {
      padding-right: 0;
    }
  }

  article {
    .header {
      text-align: center;
      .title {
        font-size: 30px;
        font-weight: normal;
      }
      .info {
        font-size: 14px;
      }
      .tags {
        margin-top: 20px;
      }
    }
    .content {
      margin-top: 50px;
    }
  }

  .comment {
    margin-top: 50px;
  }
</style>
